/**
 * Created by elewen on 2017/5/26.
 */
(function () {
    app
        .controller('myDetailCtrl', ['$rootScope', '$scope', '$state', 'AuthService', 'RESTService', function ($rootScope, $scope, $state, AuthService, RESTService) {
            $rootScope.selecInd = 6;
            $rootScope.showTabs = true;

            $scope.logout = function () {
                AuthService.logout();
            };
            RESTService.getResponse('/rest/usercoupon/getMyCouponsCount?').then(function (res) {
                $scope.myCouponCount = res.data[0].count;
            });
        }])
        .controller('redeemCtrl', ['$rootScope', '$scope', 'RESTService', function ($rootScope, $scope, RESTService) {
            $rootScope.showTabs = false;
            RESTService.getResponse('/rest/coupon/getAvailableCoupons?').then(function (response) {
                console.log(response.data);
                $scope.redeem = response.data;
                // $rootScope.$broadcast('redeem', $scope.redeem);
            });
            RESTService.getResponse('/rest/usercoupon/getMyCouponsCount?').then(function (res) {
                $scope.myCouponCount = res.data[0].count;
            });
            $scope.getCoupon = function (id, ind) {
                console.log(id);
                RESTService.getResponse('/rest/usercoupon/receiveCoupon?couponId=' + id).then(function (res) {
                    console.log(res);
                    $scope.redeem[ind].receivedCount++;
                    $scope.myCouponCount += 1;
                })
            };

        }])
        .controller('myCouponCtrl', ['$rootScope', '$scope', 'RESTService', function ($rootScope, $scope, RESTService) {
            $rootScope.showTabs = false;
            RESTService.getResponse('/rest/usercoupon/getMyCoupons?').then(function (response) {
                console.log(response.data);
                $scope.coupon = response.data;
            })
        }])
        .controller('depositCtrl', ['$rootScope', '$scope', 'RESTService', '$state', function ($rootScope, $scope, RESTService, $state) {
            $rootScope.showTabs = false;
            console.log('充值页面');
            $scope._scope = {
                selectedProduct: {},
                selectedCoupon: {
                    id: ''
                },
                currentOrder: {}
            };
            $scope._scope.currentOrder = $scope._record;
            RESTService.getResponse('/rest/product/getProducts?').then(function (res) {
                $scope.products = res.data;
            });

            $scope.selectOrder = function (ind) {
                $scope._scope.selectedProduct = $scope.products[ind];
                RESTService.getResponse('/rest/usercoupon/getMyAvailableCoupons?productId=' + $scope._scope.selectedProduct.id).then(function (res) {
                    $scope.availableCoupons = res.data;
                    $scope._scope.selectedCoupon = res.data[0] || {id: ''}


                    $state.go('app.me.deposit.create');
                });

            };

            $scope.createOrder = function (_info) {
                // console.log($scope._scope.selectedProduct, $scope._scope.selectedCoupon);
                // return
                RESTService.getResponse('/rest/order/createOrder?productId=' + $scope._scope.selectedProduct.id + '&userCouponId=' + $scope._scope.selectedCoupon.id).then(function (res) {
                    console.log(res);
                    if (res.code != 0) {
                        RESTService.pop('error', '', res.message);
                        return;
                    }
                    $scope._scope.currentOrder = res.data[0];

                    $state.go('app.me.deposit.pay');
                });
            };

            $scope.payOrder = function () {
                RESTService.getResponse('/rest/order/payOrder?orderId=' + $scope._scope.currentOrder.id).then(function (res) {
                    console.log('支付订单', res);
                    if (res.code != 0) {
                        RESTService.pop('error', '', res.message);
                        return;
                    }
                    $scope._scope.currentOrder = res.data[0];
                    $rootScope.user.validTime = $scope._scope.currentOrder.validTime;
                    $rootScope.user.isMember = true;
                    $state.go('app.me.deposit.finish');
                });
                console.log('payOrder');
            };

            $scope.cancelOrder = function () {
                RESTService.getResponse('/rest/order/cancelOrder?orderId=' + $scope._scope.currentOrder.id).then(function (res) {
                    console.log('取消订单', res);
                    if (res.code != 0) {
                        RESTService.pop('error', '', res.message);
                        return;
                    }
                    $scope._scope.currentOrder = res.data[0];
                });
                console.log('cancelOrder');
            }
        }])
        .controller('depositRecordCtrl', ['$rootScope', '$scope', 'RESTService', '$state', function ($rootScope, $scope, RESTService, $state) {
            $rootScope.showTabs = false;
            RESTService.getResponse('/rest/order/getOrders?').then(function (res) {
                $scope.records = res.data;
            });
            $scope.recordPay = function (id, ind) {
                $scope.$parent._record = $scope.records[ind];
                $state.go('app.me.deposit.pay');
            };
            $scope.recordCancel = function (id, ind) {
                RESTService.getResponse('/rest/order/cancelOrder?orderId=' + id).then(function (res) {
                    if (res.code != 0) {
                        RESTService.pop('error', '', res.message);
                        return;
                    }
                    $scope.records[ind] = res.data[0];
                });
            };
        }])
        .controller('updateInfoCtrl', ['$rootScope', '$scope', 'RESTService', '$state', function ($rootScope, $scope, RESTService, $state) {
            $rootScope.showTabs = false;
            $scope.newRealName = $scope.user.realName;
            $scope.updateUser = function () {
                if ($scope.user.realName === $scope.newRealName || $scope.newRealName == '') {
                    return;
                }

                RESTService.getResponse('/rest/user/updateUser?userId=0&realName=' + $scope.newRealName).then(function (res) {
                    if (res.code != 0) {
                        RESTService.pop('error', '', res.message);
                        return;
                    }

                    $rootScope.user = res.data[0];
                    $state.go('app.me.detail');
                });
            }

        }])
        .controller('updatePasswordCtrl', ['$rootScope', '$scope', 'RESTService', '$state', function ($rootScope, $scope, RESTService, $state) {
            $rootScope.showTabs = false;
            $scope.updatePwd = function (info) {
                console.log(info);
                if (info.oldPassword == '' || info.newPassword !== info.newRepassword) {
                    return;
                }

                RESTService.getResponse('/rest/user/updatePassword?oldPassword=' + info.oldPassword + '&newPassword=' + info.newPassword).then(function (res) {
                    console.log(res);
                    if (res.code != 0) {
                        RESTService.pop('error', '', res.message);
                        return;
                    }

                    $scope._user = {};
                    $state.go('app.me.detail');
                    RESTService.pop('success', '', '密码修改成功');
                    // $rootScope.user = res.data[0];
                });

                console.log({
                    realName: $scope.user.realName,
                    newRealName: $scope.newRealName
                });
            }

        }])
})();